'use strict'

const moment = require('moment')
const Service = require('egg').Service

class MsgAckService extends Service {
  /**
   * 获取我的聊天消息确认回执记录
   * @param {*} param0 
   * @returns 
   */
  async list({ user_id }) {
    const sql = `select * from t_chat_room_user_ack where user_id = ${user_id}`
    const record = await this.app.mysql.query(sql);
    return record;
  }

  /**
   * 创建消息回执
   * @param {*} param0 
   * @returns 
   */
  async create({ user_id, partner_id, ack_time }) {
    ack_time = Date.now()
    // ack_time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
    // ack_time = new Date();
    const querySql = `
      select * from t_chat_room_user_ack
      where user_id = ${user_id} and partner_id = '${partner_id}'
    `
    const record = await this.app.mysql.query(querySql);

    if (record.length) {
      const sql = `
        update t_chat_room_user_ack set ack_time = ${ack_time}
        where id = ${record[0].id}
      `
      await this.app.mysql.query(sql);
      return {
        user_id,
        partner_id,
        ack_time
      }
    } else {
      const sql = `
        insert into
        t_chat_room_user_ack(partner_id, user_id, ack_time)
        values('${partner_id}', ${user_id}, ${ack_time})
      `
      await this.app.mysql.query(sql);
      return {
        user_id,
        partner_id,
        ack_time
      }
    }
  }
}

module.exports = MsgAckService;
